package org.xlx.xlx_oa.warehouse.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xlx.xlx_oa.common.BusinessException;
import org.xlx.xlx_oa.warehouse.convertor.WarehouseConvertor;
import org.xlx.xlx_oa.warehouse.domain.Warehouse;
import org.xlx.xlx_oa.warehouse.domain.WarehouseDistributor;
import org.xlx.xlx_oa.warehouse.domain.WarehouseFacility;
import org.xlx.xlx_oa.warehouse.domain.dto.DistributorDto;
import org.xlx.xlx_oa.warehouse.domain.dto.FacilityDto;
import org.xlx.xlx_oa.warehouse.domain.vo.FacilityVo;
import org.xlx.xlx_oa.warehouse.mapper.WarehouseDistributorMapper;
import org.xlx.xlx_oa.warehouse.mapper.WarehouseFacilityMapper;
import org.xlx.xlx_oa.warehouse.mapper.WarehouseMapper;

import javax.management.Query;
import java.util.List;
import java.util.Objects;

@Service
public class WarehouseFacilityService {
    private final WarehouseConvertor warehouseConvertor = WarehouseConvertor.INSTANCE;
    @Autowired
    private WarehouseFacilityMapper warehouseFacilityMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private WarehouseDistributorMapper distributorMapper;

    /**
     * 判断仓库是否为空
     *
     * @param warehouseId
     * @return
     */
    public Boolean checkWarehouseEmpty(Long warehouseId) {
        QueryWrapper<WarehouseFacility> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WarehouseFacility::getWarehouseId, warehouseId);
        List<WarehouseFacility> facilities = warehouseFacilityMapper.selectList(queryWrapper);
        return facilities.size() == 0;
    }

    /**
     * 保存设备
     */
    public Boolean saveFacility(FacilityVo facilityVo) {
        WarehouseFacility facility = warehouseConvertor.convertVoToFacility(facilityVo);
        Long id = facilityVo.getId();
        if (Objects.isNull(id)) {
            warehouseFacilityMapper.insert(facility);
        } else {
            warehouseFacilityMapper.updateById(facility);
        }
        return true;
    }

    /**
     * 删除设备
     */
    public Boolean removeFacility(Long id) {
        WarehouseFacility facility = warehouseFacilityMapper.selectById(id);
        if (facility.getNowCapacity()!=0){
            throw new BusinessException("100015","There is still stock, please empty it first");
        }
        return warehouseFacilityMapper.deleteById(id) > 0;
    }

    /**
     * 设备列表
     */
    public List<FacilityDto> getList(FacilityVo facilityVo) {
        Integer pageSize = facilityVo.getPageSize();
        Integer pageNum = facilityVo.getPageNum();
        QueryWrapper<WarehouseFacility> wrapper = new QueryWrapper<>();
        Page<WarehouseFacility> page = new Page<>(pageNum, pageSize);
        if (facilityVo.getWarehouseId()!=null){
            wrapper.lambda().eq(WarehouseFacility::getWarehouseId,facilityVo.getWarehouseId());
        }
        if (facilityVo.getDistributorId()!=null){
            wrapper.lambda().eq(WarehouseFacility::getDistributorId,facilityVo.getDistributorId());
        }
        warehouseFacilityMapper.selectPage(page,wrapper);
        List<FacilityDto> facilityDtos = warehouseConvertor.convertToFacilityDtoList(page.getRecords());
        facilityDtos.forEach(facilityDto -> this.supplyFacilityDto(facilityDto));
        return facilityDtos;
    }

    /**
     * 设备总数
     * @param facilityVo
     * @return
     */
    public Integer getCount(FacilityVo facilityVo){
        QueryWrapper<WarehouseFacility> wrapper = new QueryWrapper<>();
        if (facilityVo.getWarehouseId()!=null){
            wrapper.lambda().eq(WarehouseFacility::getWarehouseId,facilityVo.getWarehouseId());
        }
        if (facilityVo.getDistributorId()!=null){
            wrapper.lambda().eq(WarehouseFacility::getDistributorId,facilityVo.getDistributorId());
        }
        return warehouseFacilityMapper.selectCount(wrapper);
    }

    /**
     * 补全计划
     * @param facilityDto
     */
    public void supplyFacilityDto(FacilityDto facilityDto){
        if (facilityDto.getWarehouseId()!=null&&facilityDto.getWarehouseId()!=0){
            Warehouse warehouse = warehouseMapper.selectById(facilityDto.getWarehouseId());
            facilityDto.setWarehouseTitle(warehouse.getTitle());
        }
        if (facilityDto.getDistributorId()!=null&&facilityDto.getDistributorId()!=0){
            WarehouseDistributor distributor = distributorMapper.selectById(facilityDto.getDistributorId());
            facilityDto.setCompanyName(distributor.getCompanyName());
        }
    }

    /**
     * 设备详情
     */
    public FacilityDto getDetail(Long id) {
        return warehouseFacilityMapper.selectFacilityOne(id);
    }

    /**
     * 模糊查询
     */
    public List<FacilityDto> searchByTitle(String title){
        QueryWrapper<WarehouseFacility> queryWrapper = new QueryWrapper<>();
        if (title!=null&&(!"".equals(title))) {
            queryWrapper.lambda().like(WarehouseFacility::getTitle, title);
        }
        List<WarehouseFacility> facilities = warehouseFacilityMapper.selectList(queryWrapper);
        List<FacilityDto> facilityDtos = warehouseConvertor.convertToFacilityDtoList(facilities);
        facilityDtos.forEach(facilityDto -> this.supplyFacilityDto(facilityDto));
        return facilityDtos;
    }
}
